// pages/psi/sell/search/index.js
import Dialog from 'tdesign-miniprogram/dialog';
import Toast from 'tdesign-miniprogram/toast';
//import SSE_WX from '../../model/testAI';


import pageUtil from '../../../../utils/pageUtil'

//获取应用实例
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    cityText: '得一药房',
    cityValue: [],
    defaultCityValue: '1',
    shopText: '全部',
    shopValue: [],
    sellerText: '',
    sellerValue: [],
    psiDistText: '',
    psiDistValue: [],
    produceText: '',
    produceValue: [],
    userIdentity: '', // 企业id
    userId: '', // 用户id

    citys: [
      //{ label: '全部', value: '0' },
      {
        label: '得一药房',
        value: '1'
      },
    ],
    shops: [{
        shop_name: '全部',
        shop_code: '0'
      },
      // { shop_name: '门店1', shop_code: '1' },
      // { shop_name: '门店2', shop_code: '2' },
    ],
    shopKeys: {
      label: 'shop_name',
      value: 'shop_code'
    },
    sellers: [{
      seller_name: '',
      seller_code: ''
    }],
    sellerKeys: {
      label: 'seller_name',
      value: 'seller_code'
    },
    psiDists: [
      // { label: '进货', value: '1' },
      {
        label: '销售',
        value: '20'
      },
      {
        label: '销售退货',
        value: '21'
      },
    ],
    produces: [{
        label: '产品1',
        value: '1'
      },
      {
        label: '产品1',
        value: '2'
      },
    ],
    produceKeys: {
      label: 'prd_name',
      value: 'prd_code'
    },
    years: [{
        label: '2021年',
        value: '2021'
      },
      {
        label: '2020年',
        value: '2020'
      },
      {
        label: '2019年',
        value: '2019'
      },
    ],
    seasons: [{
        label: '1月',
        value: '1'
      },
      {
        label: '2月',
        value: '2'
      },
      {
        label: '3月',
        value: '3'
      },
      {
        label: '4月',
        value: '4'
      },
      {
        label: '5月',
        value: '5'
      },
      {
        label: '6月',
        value: '6'
      },
      {
        label: '7月',
        value: '7'
      },
      {
        label: '8月',
        value: '8'
      },
      {
        label: '9月',
        value: '9'
      },
      {
        label: '10月',
        value: '10'
      },
      {
        label: '11月',
        value: '11'
      },
      {
        label: '12月',
        value: '12'
      },
    ],
    mode: '',
    dateVisible: false,
    date: new Date('2021-12-23').getTime(), // 支持时间戳传入
    dateText: '',
    // 指定选择区间起始值
    start: '2000-01-01 00:00:00',
    end: '2030-09-09 12:12:12',
    dataContent: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("---111 sell---");
    // 检验是否登录
    app.checkLogin();
    // 获取身份权限
    this.setData({
      userIdentity: app.globalData.userIdentity,
      userId: app.globalData.userId,
    });

    console.log("---onLoad---");

    const current_page = pageUtil.getCurrentPageUrl()
    console.log("当前页路径", current_page);

    // 记录操作
    app.api.log({
      title: '实时查询',
      message: 'success',
    })

    // 获取字典
    this.getDistShop();
    this.getDistSeller();
    this.getDistProduce();

    // 设置默认值
    const {
      value,
      label
    } = this.data.citys?.[0];
    //console.log('this.citys', this.data.citys[0]);
    this.setData({
      cityValue: value,
      cityText: label,
    });
  },

  getDistShop() {
    console.log('getDistShop');
    wx.request({
      url: 'https://sst.jmit.cn/api_sst/getDistShop.aspx',
      data: '',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
      },
      success: (request) => {
        console.log('getDistShop的结果', request.data);
        const res = request.data;
        //app.globalData.userInfo = res.data.data;//
        //var data = JSON.parse(res.data);

        if (res.code == 200) {
          this.setData({
            shops: res.data
          });
        } else {}
      },
      fail(res) {
        console.log(res.erMsg);
      },
    });
  },
  getDistSeller() {
    console.log('getDistSeller');
    this.setData({
      sellerText: '',
      sellerValue: [],
    });
    const {
      shopText
    } = this.data;
    console.log('选中的门店：' + shopText);

    wx.request({
      url: 'https://sst.jmit.cn/api_sst/getDistSeller.aspx',
      data: {
        shop_code: shopText,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
      },
      success: (request) => {
        console.log('getDistSeller的结果', request.data);
        const res = request.data;
        //app.globalData.userInfo = res.data.data;//
        //var data = JSON.parse(res.data);

        if (res.code == 200) {
          this.setData({
            sellers: res.data
          });
        } else {}
      },
      fail(res) {
        console.log(res.errMsg);
      },
    });
  },
  getDistProduce() {
    console.log('getDistProduce');
    const userIdentity = this.data.userIdentity; // 身份权限
    const userId = this.data.userId; // 身份权限
    wx.request({
      url: 'https://sst.jmit.cn/api_sst/getDistPrd.aspx',
      data: {
        user_identity: userIdentity,
        user_id: userId,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
      },
      success: (request) => {
        console.log('getDistProduce的结果', request.data);
        const res = request.data;
        //app.globalData.userInfo = res.data.data;//
        //var data = JSON.parse(res.data);

        if (res.code == 200) {
          this.setData({
            produces: res.data
          });
        } else {}
      },
      fail(res) {
        console.log(res.erMsg);
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().init();
  },

  onColumnChange(e) {
    console.log('picker pick:', e);
  },

  onPickerChange(e) {
    const {
      key
    } = e.currentTarget.dataset;
    const {
      value,
      label
    } = e.detail;

    console.log('picker change:', e.detail);
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: label.join(' '),
    });

    console.log('key:', key);
    if (key == 'shop') {
      console.log('更新收银员');
      this.getDistSeller();
    }
  },

  onPickerCancel(e) {
    const {
      key
    } = e.currentTarget.dataset;
    console.log(e, '取消');
    console.log('picker1 cancel:');
    this.setData({
      [`${key}Visible`]: false,
    });
  },

  onCityPicker() {
    this.setData({
      cityVisible: true
    });
  },

  onShopPicker() {
    this.setData({
      shopVisible: true
    });
  },
  onSellerPicker() {
    this.setData({
      sellerVisible: true
    });
  },

  onPsiDistPicker() {
    this.setData({
      psiDistVisible: true
    });
  },

  onProducePicker() {
    this.setData({
      produceVisible: true
    });
  },

  onSeasonPicker() {
    this.setData({
      dateVisible: true
    });
  },

  showPicker(e) {
    const {
      mode
    } = e.currentTarget.dataset;
    this.setData({
      mode,
      [`${mode}Visible`]: true,
    });
  },
  hidePicker() {
    const {
      mode
    } = this.data;
    this.setData({
      [`${mode}Visible`]: false,
    });
  },
  onConfirm(e) {
    const {
      value
    } = e.detail;
    const {
      mode
    } = this.data;

    console.log('confirm', value);

    this.setData({
      [mode]: value,
      [`${mode}Text`]: value,
    });

    this.hidePicker();
  },

  onColumnChange(e) {
    console.log('pick', e.detail.value);
  },

  // 提交订单
  submitOrder() {
    const {
      cityText,
      shopText,
      shopValue,
      produceValue,
      sellerValue,
      psiDistValue,
      dayBegin,
      dayEnd
    } = this.data;
    // const { goodsRequestList } = this;

    // console.log(!shopText);
    console.log(shopText);
    console.log(shopValue);

    if (!shopText && shopValue.length === 0) {
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请选择门店',
        duration: 2000,
        icon: 'help-circle',
      });

      return;
    }

    wx.navigateTo({
      url: `/pages/psi/sell/result/index?customerCode=${cityText}&shopCode=${shopValue}&prdCode=${produceValue}&psiDist=${psiDistValue}&sellerCode=${sellerValue}`,
    });
  },





  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});